//
//  SearchModellCell.swift
//  ezyform
//
//  Created by Moennig Q on 8/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit

class SearchModellCell: UITableViewCell {
    
    var modelLabel: BaseLabel!
    var brandLabel: BaseLabel!
    var wattsLabel: BaseLabel!
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpviews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if selected {
            self.contentView.backgroundColor = UIColor(hexStr: "#E7F9F2")
        }else {
            self.contentView.backgroundColor = UIColor.white
        }
    }
    private func setUpviews(){
        self.selectionStyle = .none
        //model
        modelLabel = ContentLabel(R.font.interMedium(size: 15))
        modelLabel.numberOfLines = 2
        self.contentView.addSubview(modelLabel)
        modelLabel.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(16)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //watts
        wattsLabel = ContentLabel(R.font.interMedium(size: 15),hexStr: "#292D32B2")
        wattsLabel.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        wattsLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        self.contentView.addSubview(wattsLabel)
        wattsLabel.snp.makeConstraints { make in
            make.centerY.equalTo(modelLabel)
            make.right.equalTo(-20)
            make.left.equalTo(modelLabel.snp.right)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //brand
        brandLabel = ContentLabel(R.font.interRegular(size: 12),hexStr: "#7C7C7CFF")
        brandLabel.numberOfLines = 0
        self.contentView.addSubview(brandLabel)
        brandLabel.snp.makeConstraints { make in
            make.top.equalTo(modelLabel.snp.bottom).offset(4)
            make.left.equalTo(modelLabel)
            make.right.equalTo(wattsLabel)
            make.height.width.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview().offset(-12)
        }
    }
    func setData(_ model: SearchDictionaryModel, type: SearchModelType){
        self.brandLabel.text = model.name
        self.modelLabel.text = model.model_number
        //
        var powerText = ""
        //Panel
        if type == .panel && model.watts != 0{
            powerText = "\(model.watts)W"
        }else if type == .inverter  && model.ac_power != 0{//inverter,
            powerText = "\(model.ac_power)W"
        }else if type == .battery && model.battery_capacity != ""{//battery
            powerText = "\(model.battery_capacity)kWh"
        }
        self.wattsLabel.text = powerText
    }
}
